package cn.usian.jyzx.product.service.impl;

import cn.usian.jyzx.model.entity.product.Category;
import cn.usian.jyzx.product.mapper.CategoryMapper;
import cn.usian.jyzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public List<Category> findCategoryTree() {
       List<Category> all = categoryMapper.findCategoryTree();

        List<Category> oneall = all.stream().filter(category -> category.getParentId().equals(0L)).collect(Collectors.toList());
        //       遍历一级分类，取二类
        oneall.forEach(one -> {
            List<Category> twoall = all.stream().filter(category -> category.getParentId().equals(one.getId())).collect(Collectors.toList());
//            遍历二级集合，取三类
            twoall.forEach(two->{
                List<Category> threeall = all.stream().filter(category -> category.getParentId().equals(two.getId())).collect(Collectors.toList());
                two.setChildren(threeall);
            });
            one.setChildren(twoall);
        });
        return oneall;
    }
}
